package com.example.back.mapper;

import com.example.back.dto.req.DoctorListReq;
import com.example.back.dto.resp.DepList;
import com.example.back.dto.resp.DoctorListRsp;
import com.example.common.entity.PlantOrder;

import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

@Component
public interface DoctorMapper {

    @SelectProvider(type =DoctorMapper.CommonProvider.class,method = "list")
    List<DoctorListRsp> List(@Param("doctorListReq") DoctorListReq doctorListReq);

    @Select("select a.id_bigint AS ID, a.name_nvarchar AS Name from h_hospital_department a where " +
            "Disable_bit = 0 and not exists " +
            "(select 1 from h_hospital_department b where b.pd_id_bigint = a.ID_bigint)")
    List<DepList> findAllDepart();

    @Select("select plant_time_date as plantTime,time_type_tinyint as timeType,count_order_int as countOrder,sell_count_int as sellCount from doctor_plant_order"+
    " where dc_id_bigint = #{dcId} and disable_bit = 0 and plant_time_date >= #{beg} and plant_time_date < #{end} order by plant_time_date,time_type_tinyint")
    List<PlantOrder> getPlantList(@org.apache.ibatis.annotations.Param("dcId") long dcId, @org.apache.ibatis.annotations.Param("beg") Date beg, @org.apache.ibatis.annotations.Param("end") Date end);

    class  CommonProvider{
        public String list(DoctorListReq doctorListReq){
            SQL sql = new SQL();
            sql.SELECT("a.dc_id AS DCID,a.`name` AS Name,c.name_nvarchar AS DepName ," +
                    "a.title AS Title,a.phone AS Phone, a.`disable` AS Disable " +
                    "FROM doctor_base_info a INNER JOIN h_hospital_department c ON(a.depart_id=c.id_bigint)");

            if (!ObjectUtils.isEmpty(doctorListReq.getName())){
                String name = doctorListReq.getName();
                sql.WHERE("a.name LIKE CONCAT ('%',#{name},'%')");
            }
            sql.ORDER_BY("a.create_time DESC");
            return sql.toString();
        }
    }
}
